package io.sdl.common.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.sdl.common.service.impl.CrudServiceImpl;
import io.sdl.common.sys.dao.SdlSessionMessageDao;
import io.sdl.common.sys.dto.SdlSessionMessageDTO;
import io.sdl.common.sys.entity.SdlSessionMessageEntity;
import io.sdl.common.sys.service.SdlSessionMessageService;
import io.sdl.common.utils.ConvertUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("sdlSessionMessageService")
public class SdlSessionMessageServiceImpl extends CrudServiceImpl<SdlSessionMessageDao, SdlSessionMessageEntity, SdlSessionMessageDTO> implements SdlSessionMessageService {


    @Override
    public QueryWrapper<SdlSessionMessageEntity> getWrapper(Map<String, Object> params) {
        return null;
    }

    public void saveMessage(SdlSessionMessageEntity message) {
        message.setSendTime(new Date());
        baseDao.insert(message);
    }

    public List<SdlSessionMessageEntity> getRecentMessages(String roomId, long since) {
        return baseDao.findByRoomIdAndTimestampGreaterThan(roomId, since);
    }

    @Override
    public List<SdlSessionMessageEntity> getUserSessions(Long userId) {
        return baseDao.getUserSessions(userId);
    }


    //根据会话id获取历史消息
    @Override
    public List<SdlSessionMessageDTO> getHistoryMessages(Long sessionId,Integer page , Integer pageSize) {


        return ConvertUtils.sourceToTarget(baseDao.getHistoryMessages(sessionId,page!=null?(page-1)*pageSize:null,pageSize),SdlSessionMessageDTO.class);
    }

    //获取当前登录用户的聊天列表及最新消息和未读消息数量
    @Override
    public List<SdlSessionMessageDTO> getMsgList(Long receiverId) {

        return baseDao.getMsgList(receiverId);
    }

    @Override
    public Integer readMsg(Long receiverId, Long senderId) {
        return baseDao.readMsg(receiverId,senderId);
    }

    @Override
    public int getUnreadMsg(Long receiverId) {
        return baseDao.getUnreadMsg(receiverId);
    }

    @Override
    public List<SdlSessionMessageEntity> verificationCard(String patientCard) {
        return baseDao.verificationCard(patientCard);
    }
}